import {Divider, message} from 'antd';
import React, {useState, useRef} from 'react';
import {PageContainer} from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';

import {queryRule, stockAudit} from './service';

const TableList = () => {
  const actionRef = useRef();
  const columns = [
    {
      title: '店名',
      hideInSearch: true,
      dataIndex: 'storeName',
      render: (dom, entity) => {
        return <a>{dom}</a>;
      },
    },
    {
      title: '商品名',
      hideInSearch: true,
      dataIndex: 'goodsItemName',
    },
    {
      title: '上库人',
      hideInSearch: true,
      dataIndex: 'createName',
    },
    {
      title: '审核状态',
      dataIndex: 'status',
      hideInForm: true,
      valueEnum: {
        0: {
          text: '未审核',
          status: 'Processing',
        },
        1: {
          text: '通过',
          status: 'Success',
        },
        2: {
          text: '拒绝',
          status: 'Error',
        },
      },
    },
    {
      title: '上库数量',
      hideInSearch: true,
      dataIndex: 'inCount',
      hideInForm: true,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => (
        <>
          {record.status === 0 ? (
            <>
              <a
                onClick={() => {
                  const params = {
                    id: record.id,
                    audit: true
                  };
                  stockAudit(params).then(resp => {
                    if (resp) {
                      if (resp.code === 0) {
                        message.info("审核成功,上库通过")
                      } else {
                        message.warning(resp.message)
                      }
                    }
                    actionRef.current.reload();
                  })
                }}
              >
                通过
              </a>
              <Divider type="vertical"/>
              <a
                onClick={() => {
                  const params = {
                    id: record.id,
                    audit: false
                  };
                  stockAudit(params).then(resp => {
                    if (resp) {
                      if (resp.code === 0) {
                        message.info("审核成功,上库拒绝")
                      } else {
                        message.warning(resp.message)
                      }
                    }
                    actionRef.current.reload();
                  })
                }}
              >
                拒绝
              </a>
            </>
          ) : null
          }
        </>
      ),
    },
  ];
  return (
    <PageContainer>
      <ProTable
        headerTitle="上库审核列表"
        actionRef={actionRef}
        rowKey="id"
        search={{
          labelWidth: 120,
        }}
        scroll={{x: 1200}}
        bordered
        request={(params, sorter, filter) => queryRule({...params, sorter, filter})}
        postData={(payload) => {
          const {list, total} = payload;
          return list || [];
        }}
        columns={columns}
      />
    </PageContainer>
  );
};

export default TableList;
